package com.nightpiter.view.drawer.dynamic;

import static com.nightpiter.view.drawer.rapid.Config.hubRadius;
import static com.nightpiter.view.drawer.rapid.Config.stationBackRadius;
import static com.nightpiter.view.drawer.rapid.Config.stationRadius;

import java.util.ArrayList;
import java.util.HashMap;

import com.nightpiter.model.objects.Station;
import com.nightpiter.view.Calculator;
import com.nightpiter.view.graphics.Canvas;
import com.nightpiter.view.graphics.Color;
import com.nightpiter.view.graphics.PointF;
import com.nightpiter.view.graphics.RadialGradient;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * 
 */
class PrimitiveAmoeba extends Amoeba {

	@SuppressWarnings("unused")
	private String name;

	public PrimitiveAmoeba(AmoebaUltimate amoebaUltimate, Station station,
			int color, PointF coordF) {
		this.name = station.getName();
		this.amoebaUltimate = amoebaUltimate;
		this.size = 1;
		this.coordF = coordF;
		this.stations = new Station[1];
		this.colors = new int[1];
		this.coords = new PointF[1];
		this.stations[0] = station;
		this.colors[0] = color;
		this.coords[0] = coordF;
	}

	/** neighbor-to-link mapping */
	private HashMap<PrimitiveAmoeba, PrimitiveBacillus> primitiveBacillusMap = new HashMap<PrimitiveAmoeba, PrimitiveBacillus>();

	public PrimitiveBacillus searchPrimitiveBacillus(
			PrimitiveAmoeba neighborPrimitiveAmoeba) {
		PrimitiveBacillus primitiveBacillus = primitiveBacillusMap
				.get(neighborPrimitiveAmoeba);
		if (primitiveBacillus == null) {
			BacillusUltimate bacillusUltimate = this.amoebaUltimate
					.searchBacillusUltimate(neighborPrimitiveAmoeba.amoebaUltimate);
			primitiveBacillus = new PrimitiveBacillus(bacillusUltimate, this,
					neighborPrimitiveAmoeba);
		}
		return primitiveBacillus;
	}

	void addNeighborPrimitive(PrimitiveAmoeba neighborPrimitiveAmoeba,
			PrimitiveBacillus primitiveBacillus) {
		primitiveBacillusMap.put(neighborPrimitiveAmoeba, primitiveBacillus);
		super.addNeighbor(neighborPrimitiveAmoeba, primitiveBacillus);
	}

	public AmoebaUltimate getUltimateAmoeba() {
		return amoebaUltimate;
	}

	@Override
	protected void applyCalculator(Calculator calculator) {
		calculator.convert(coordF, pointF);
	}

	@Override
	protected void addDrawnAmoebas(ArrayList<Amoeba> drawnAmoebas) {
		drawnAmoebas.add(this);
	}

	@Override
	protected void draw(Canvas canvas, boolean direct) {
		if (direct) {
			canvas.drawPoint(pointF, backSimplePaint);
			simplePaint.setShader(new RadialGradient(pointF.x + 1,
					pointF.y + 1, (float) (stationRadius * 1.3), Color.WHITE,
					colors[0]));
			canvas.drawPoint(pointF, simplePaint);
		} else {
			canvas.drawPoint(pointF, backComplexPaint);
			complexPaint.setShader(new RadialGradient(pointF.x + 1,
					pointF.y + 1, (float) (hubRadius * 1.3), Color.WHITE,
					colors[0]));
			canvas.drawCircle(pointF, hubRadius / 2, complexPaint);

		}
	}

	public static void setup() {
		simplePaint.setStrokeWidth(stationRadius * 2);
		backSimplePaint.setStrokeWidth(stationBackRadius * 2);
	}

}
